package util.nlp;

import java.util.Hashtable;

public class Dictionary {

	private SimpleTrie trie = new SimpleTrie();

	public Hashtable<String, String> hash = new Hashtable<String, String>();

	
	public void addItem(String string, String original) {

		
		hash.put(string, original);
		trie.add(string);

	}

	public String getLongestMatch(String m) {

		if (m.equals(""))
			return null;

		int index = m.indexOf("/");

		if (index < 1) {

			index = m.length();
		}
		String domain = m.substring(0, index);

		String[] as = trie.suggest(domain);

		// return longests prefix of the target url

		String temporal = "";

		for (int i = 0; as != null && i < as.length; i++) {

			if (!as[i].equals("") && m.startsWith(as[i]) && trie.isEntry(as[i])) {
				if (temporal.length() < as[i].length()) {

					temporal = as[i];
				}

			}

		}

		if (temporal.equals(""))
			return null;

		return hash.get(temporal);

	}

	public boolean containsURL(String m) {

		if (m.equals(""))
			return false;

		int index = m.indexOf("/");

		if (index < 1) {

			index = m.length();
		}
		String domain = m.substring(0, index);

		String[] as = trie.suggest(domain);

		// check m for urls in dict that match the host
		for (int i = 0; as != null && i < as.length; i++) {

			if (!as[i].equals("") && m.startsWith(as[i]) && trie.isEntry(as[i])) {

				return true;

			}

		}

		return false;

	}
	
	public static void main(String args[]){
		
		Dictionary dic  = new Dictionary();
		
		dic.addItem("star wars asfasd", "star_wars");
		dic.addItem("wars stars lego adsfa", "lego_star_wars");
		dic.addItem("star wars sites adfa", "star_wars_sites");
		dic.addItem("star wars sites academy adsfa", "star_wars_sites_academy");
		dic.addItem("star wars sites academy adsfa", "star_wars_sites_academy");

		String[] res = dic.trie.suggest("nothin");
		
		for(int i=0; i < res.length; i++){
			
			
			System.out.println(res[i]);
		}
		
	}

}